package hu.szyrt.bug.adapters;

import hu.szyrt.bug.R;
import hu.szyrt.bug.models.SubCategory;
import hu.szyrt.bug.utils.Assets;

import java.util.ArrayList;
import java.util.List;

import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SubCategoryListAdapter extends BaseAdapter{

	private List<SubCategory> items = new ArrayList<SubCategory>();
	private ViewHolder holder;
	private Assets assets;
	
	public SubCategoryListAdapter(Assets assets, List<SubCategory> items) {
		this.assets = assets;
		this.items = items;
	}
	
	public void changeList(List<SubCategory> items){
		this.items = items;
		notifyDataSetChanged();
	}
	
	@Override
	public int getCount() { return items.size(); }
	@Override
	public Object getItem(int position) {return items.get(position);}
	@Override
	public long getItemId(int position) { return position; }
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		SubCategory item = items.get(position);
		
		if(convertView == null){
			convertView = View.inflate(parent.getContext(), R.layout.category_list_item, null);
			holder = new ViewHolder();
			holder.title = (TextView) convertView.findViewById(R.id.title);
			holder.content = (TextView) convertView.findViewById(R.id.content);
			holder.polaroid = (ImageView) convertView.findViewById(R.id.polaroid);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder)convertView.getTag();
		}
		
		holder.title.setText(item.getName());
		holder.content.setText(item.getDescription());		 
		if(holder.polaroid.getDrawable() != null) ((BitmapDrawable)holder.polaroid.getDrawable()).getBitmap().recycle();
		holder.polaroid.setImageBitmap(assets.loadPolaroid(item.getImageName(), 3));		
		
		return convertView;		
	}
	
	static class ViewHolder{
		TextView title, content;
		ImageView polaroid;
	}
	
}

